<?php

namespace app\common\model;

class UserDailyPractice extends Base
{
    /**
     * 数据表名
     * @var string
     */
    protected $table = 'think_user_daily_practice';
    
    /**
     * 主键
     * @var string
     */
    protected $pk = 'id';
    
    /**
     * 自动时间戳
     * @var bool
     */
    protected $autoWriteTimestamp = false;
    
    /**
     * 添加每日一练记录
     * @param int $uid 用户ID
     * @param int $courseId 课程ID
     * @param int $questionId 问题ID
     * @return int|string
     */
    public function addRecord($uid, $courseId, $questionId)
    {
        $data = [
            'uid' => $uid,
            'course_id' => $courseId,
            'question_id' => $questionId,
            'create_time' => time(),
        ];
        return $this->add_data($data);
    }
    
    /**
     * 获取用户每日一练记录
     * @param int $uid 用户ID
     * @param string $field 查询字段
     * @return array
     */
    public function getUserRecords($uid, $field = '*')
    {
        $records = $this->where(['uid' => $uid])->field($field)->select();
        return $records ? $records->toArray() : [];
    }
    
    /**
     * 获取用户在某课程下的每日一练记录
     * @param int $uid 用户ID
     * @param int $courseId 课程ID
     * @param string $field 查询字段
     * @return array
     */
    public function getUserCourseRecords($uid, $courseId, $field = '*')
    {
        $records = $this->where(['uid' => $uid, 'course_id' => $courseId])->field($field)->select();
        return $records ? $records->toArray() : [];
    }
    
    /**
     * 检查用户是否已经练习过某题目
     * @param int $uid 用户ID
     * @param int $questionId 问题ID
     * @return bool
     */
    public function checkQuestionPracticed($uid, $questionId)
    {
        return (bool)$this->where(['uid' => $uid, 'question_id' => $questionId])->count();
    }
    
    /**
     * 获取用户当天在指定课程下的每日一练记录
     * @param int $uid 用户ID
     * @param int $courseId 课程ID
     * @param string $field 查询字段
     * @return array
     */
    public function getTodayCoursePractice($uid, $courseId, $field = '*')
    {
        // 获取当天的开始和结束时间戳
        $todayStart = strtotime(date('Y-m-d'));
        $todayEnd = $todayStart + 86399; // 23:59:59
        
        // 查询当天用户在该课程下的记录
        $records = $this->where([
            'uid' => $uid,
            'course_id' => $courseId
        ])->whereBetween('create_time', [$todayStart, $todayEnd])->field($field)->select();
        
        return $records ? $records->toArray() : [];
    }
    
    /**
     * 检查用户当天是否在指定课程下有每日一练记录
     * @param int $uid 用户ID
     * @param int $courseId 课程ID
     * @return bool
     */
    public function hasTodayCoursePractice($uid, $courseId)
    {
        // 获取当天的开始和结束时间戳
        $todayStart = strtotime(date('Y-m-d'));
        $todayEnd = $todayStart + 86399; // 23:59:59
        
        // 查询当天用户在该课程下的记录
        return (bool)$this->where([
            'uid' => $uid,
            'course_id' => $courseId
        ])->whereBetween('create_time', [$todayStart, $todayEnd])->count();
    }
    
    /**
     * 按照uid和course_id删除每日一练记录
     * @param int $uid 用户ID
     * @param int $courseId 课程ID
     * @return int 删除的记录数
     */
    public function deleteByUserAndCourse($uid, $courseId)
    {
        return $this->where([
            'uid' => $uid,
            'course_id' => $courseId
        ])->delete();
    }
}